package org.jmc.refmanager.web.wicket.form;

import java.util.Date;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.datetime.markup.html.basic.DateLabel;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.validation.validator.EmailAddressValidator;
import org.jmc.refmanager.domain.model.OrgUnit;
import org.jmc.refmanager.domain.model.OrgUnit.Complement;
import org.jmc.refmanager.jsr303validators.JSR303FormValidator;
import org.jmc.refmanager.web.wicket.markup.html.form.ChoiceRenderer.EnumDropDownChoice;

public abstract class ServiceEditForm extends Form<OrgUnit> {
	private static final long serialVersionUID = 1L;

	public ServiceEditForm(String id, IModel<OrgUnit> model, final ModalWindow codePostalPicker, final ModalWindow openParentPicker) {
		super(id, model);
		setModel(new CompoundPropertyModel<OrgUnit>(model));
		setOutputMarkupId(false);
		OrgUnit service = getModelObject();

		add(new Label("codeUO", new PropertyModel<String>(model, "codeUO")));
		add(new RequiredTextField<String>("libCourt", 
				new PropertyModel<String>(model, "libCourt")));
		add(new RequiredTextField<String>("libLong", 
				new PropertyModel<String>(model, "libLong")));
		add(new RequiredTextField<String>("libComplet", 
				new PropertyModel<String>(model, "libComplet")));
		add(DateLabel.forDatePattern("dateEffet1", 
				new PropertyModel<Date>(model, "dateEffet1"), "dd/MM/yyyy"));

		String saaOrSao;
		if (service.isSaa()) {
			saaOrSao = "Service administratif";
		} else if (service.isSao()) {
			saaOrSao = "Service opérationnel";
		} else {
			saaOrSao = "n/a";
		}
		add(new Label("saaOrSao", saaOrSao));

		add(new Label("type", new PropertyModel<String>(model, "type")));

		OrgUnit parent = service.getParent();
		if (parent != null) {
			add(new Label("parentType", new PropertyModel<String>(model, "parent.type")));
		} else {
			add(new Label("parentCodeUO", "n/a"));
			add(new Label("parentType", "n/a"));
		}

		add(new TextField<String>("numRue", 
				new PropertyModel<String>(model, "numRue")));

		add(new EnumDropDownChoice<Complement>("complementNumRue", 
				new PropertyModel<Complement>(model, "complementNumRue"), Complement.class));

		add(new RequiredTextField<String>("adresse", 
				new PropertyModel<String>(model, "adresse")));

		add(new TextField<String>("complementAdresse", 
				new PropertyModel<String>(model, "complementAdresse")));

		add(new RequiredTextField<String>("adressePostale", 
				new PropertyModel<String>(model, "adressePostale")));

		TextField<String> email = new TextField<String>("mail", 
				new PropertyModel<String>(model, "mail"));
		email.add(EmailAddressValidator.getInstance());
		email.setRequired(service.isSaa());
		add(email);
		
		Label mailRequired = new Label("mailRequired", "*");
		mailRequired.setVisible(service.isSaa());
		add(mailRequired);

		TextField<String> telephone = new TextField<String>("telephone", 
				new PropertyModel<String>(model, "telephone"));
		telephone.setRequired(service.isSaa());
		add(telephone);

		Label telephoneRequired = new Label("telephoneRequired", "*");
		telephoneRequired.setVisible(service.isSaa());
		add(telephoneRequired);

		TextField<String> rescom = new TextField<String>("rescom", 
				new PropertyModel<String>(model, "rescom"));
		rescom.setRequired(service.isSaa());
		add(rescom);

		Label rescomRequired = new Label("rescomRequired", "*");
		rescomRequired.setVisible(service.isSaa());
		add(rescomRequired);

		AjaxLink<String> openWindowLink = new AjaxLink<String>("openWindowLink") {
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				target.appendJavascript("Wicket.Window.unloadConfirmation = false;");
				codePostalPicker.show(target);
			}
		};
		add(openWindowLink);

		AjaxLink<String> openParentCodeUOLink = new AjaxLink<String>("openParentCodeUOLink") {
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				target.appendJavascript("Wicket.Window.unloadConfirmation = false;");
				openParentPicker.show(target);
			}
		};
		add(openParentCodeUOLink);

		// Validation par la norme JSR303
		add(new JSR303FormValidator());
	}

}
